<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/bootstrap/css/bootstrap-theme.min.css">
<script src="${pageContext.request.contextPath}/js/jquery-1.11.1.js"></script>
<script
	src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">






$.post("DataBaseServlet?method=getDBs",{},
		function(result){
			var result = eval('('+result+')');
		
			if(result != null){
				for(var i = 0; i < result.length; i++){
					$("#dbName_table").append("<option value='" + result[i].id + "'>" + result[i].dbName + "</option>"); 
					$("#dbName_field").append("<option value='" + result[i].id + "'>" + result[i].dbName + "</option>"); 
				}
			}
		}
	);
	
	
$(function(){
	$("#driver").ready(function(){
		$("#dbName").change(function(){
			var sqlurl = "jdbc:sqlserver://localhost:1433;DatabaseName="+$("#dbName").val()
			var mysqlurl = "jdbc:mysql://localhost:3306/" + $("#dbName").val()
			var oraclurl = "jdbc:oracle:thin:@//:1521:"+$("#dbName").val();
			if($("#driverName :selected").val() == 0){
				$("#url").attr("value",null);  	
			}
			if($("#driverName :selected").val() == "com.mysql.jdbc.Driver"){
				$("#url").attr("value",mysqlurl);  	
			}
			if($("#driverName :selected").val() == "com.microsoft.sqlserver.jdbc.SQLServerDriver"){
				$("#url").attr("value",sqlurl);
			}
			if($("#driverName :selected").val() == "com.microsoft.jdbc.sqlserver.SQLServerDriverr"){
				$("#url").attr("value",sqlurl);
			}
			if($("#driverName :selected").val() == "oracle.jdbc.driver.OracleDriver"){
				$("#url").attr("value",oraclurl);
			}
		});
	});
})
	
	
$(function(){
	$("#dbName").ready(function(){
		$("#driverName").change(function(){
			var sqlurl = "jdbc:sqlserver://localhost:1433;DatabaseName="+$("#dbName").val();
			var mysqlurl = "jdbc:mysql://localhost:3306/" + $("#dbName").val(); 
			var oraclurl = "jdbc:oracle:thin:@//:1521:"+$("#dbName").val();
			if($("#driverName :selected").val() == 0){
				$("#url").attr("value",null);  	
			}
			if($("#driverName :selected").val() == "com.mysql.jdbc.Driver"){
				$("#url").attr("value",mysqlurl);  	
			}
			if($("#driverName :selected").val() == "com.microsoft.sqlserver.jdbc.SQLServerDriver"){
				$("#url").attr("value",sqlurl);
			}
			if($("#driverName :selected").val() == "com.microsoft.jdbc.sqlserver.SQLServerDriverr"){
				$("#url").attr("value",sqlurl);
			}
			if($("#driverName :selected").val() == "oracle.jdbc.driver.OracleDriver"){
				$("#url").attr("value",oraclurl);
			}
		});
	});
})
	
$(function(){
	$("#dbName_field").change(function(){
		var dbId = $("#dbName_field :selected").val();
		$.post("DataBaseServlet?method=getTables", {dbId: dbId},
			function(result){
				var result = eval('('+result+')');
				if(result != null){
					$("#tableid_field").empty();
					$("#tableid_field").append(" <option value='0'>请选择...</option>"); 
					for(var i = 0; i < result.length; i++){
						$("#tableid_field").append("<option value='" + result[i].id + "'>" + result[i].tableName + "</option>"); 
					}
				}
			}
		);
	});})
	
	$(function(){
		$("#submit1").click(function(){
			
			var dbName=$("#dbName").val();
			var userName = $("#userName").val();
			var password = $("#password").val();
			var url = $("#url").val();
			var driverName = $("#driverName").val();
		    if(driverName=="0"){
		    	alert("请选择驱动名！");
		    	return false;
		    }
			$.post("DataBaseServlet?method=addDBs", {dbName:dbName,userName:userName,password:password,url:url,driverName:driverName},
					function(result){
						var result = eval('('+result+')');
						if(result.conFail == "0"){
							alert("连接数据库失败，请输入正确的数据库参数");
						}else 
						if(result.Succeed == "0"){
							$.post("DataBaseServlet?method=getDBs",{},
									function(result){
										var result = eval('('+result+')');
									
										if(result != null){
											for(var i = 0; i < result.length; i++){
												$("#dbName_table").append("<option value='" + result[i].id + "'>" + result[i].dbName + "</option>"); 
												$("#dbName_field").append("<option value='" + result[i].id + "'>" + result[i].dbName + "</option>"); 
											}
										}
									}
								);
							alert("添加成功");
						}
						else{
							alert(result.Fail);
						}
							
				})
		})
	})
	/*
	添加表
	*/
	$(function(){
		$("#submit2").click(function(){
			
			var tableName=$("#tableName").val();
			var dbName_table = $("#dbName_table").val();
			$.post("DataBaseServlet?method=addTable", {tableName:tableName,dbName_table:dbName_table},
					function(result){
						var result = eval('('+result+')');
						if(result.Succeed == "0"){
							alert("添加成功");
						}
						else{
							alert(result.Fail);
						}
							
				})
		})
	})
	
	/*添加字段 */
	$(function(){
		$("#submit3").click(function(){
			
			var dbName_field=$("#dbName_field").val();
			var tableid_field = $("#tableid_field").val();
			var fieldName = $("#fieldName").val();
			var fieldType = $("#fieldType").val();
		//	var createTime = $("#createTime").val();
		//	var location = $("#location").val();
			$.post("DataBaseServlet?method=addField", {dbName_field:dbName_field,tableid_field:tableid_field,fieldName:fieldName,fieldType:fieldType},
					function(result){
						var result = eval('('+result+')');
						if(result.Succeed == "0"){
							alert("添加成功");
						}
						else{
							alert(result.Fail);
						}
							
				})
		})
	})
</script>
<title>数据管理1</title>
</head>
<body>
	<nav class="navbar navbar-inverse navbar-fixed-top">
	<div class="container">
		<div class="navbar-header">
			<a class="navbar-brand" href="Javascript: void(0)"><strong
				class="h3">数据管理&nbsp;&nbsp;</strong></a>
		</div>

		<div class="collapse navbar-collapse">
			<ul class="nav navbar-nav">
				<li id="dbs"
					onclick="change_dbs.style.display='block';change_tables.style.display='none';change_fields.style.display='none';checkresult.style.display='none'"><a
					href="Javascript: void(0)"><i class="glyphicon glyphicon-plus">&nbsp;</i>数据库&nbsp;</a></li>
				<li id="tables"
					onclick="change_dbs.style.display='none';change_tables.style.display='block';change_fields.style.display='none';checkresult.style.display='none'"><a
					href="Javascript: void(0)"><i class="glyphicon glyphicon-plus">&nbsp;</i>表&nbsp;</a></li>
				<li id="fields"
					onclick="change_dbs.style.display='none';change_tables.style.display='none';change_fields.style.display='block';checkresult.style.display='none'"><a
					href="Javascript: void(0)"><i class="glyphicon glyphicon-plus"></i>&nbsp;字段</a></li>
			</ul>

		</div>
	</div>
	</nav>

	<!-- 添加数据库 -->

	<div id="change_dbs" class="container"
		style="display: none; position: relative; top: 100px; left: 100px">

		<form id="addDbs" action="DataBaseServlet?method=addDBs" method="post">
			<div class="form-horizontal" role="form">
				<fieldset>
					<legend>添加数据库</legend>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="dbName">数据库名</label>
						<div class="col-sm-4">
							<input class="form-control" id="dbName" name="dbName" type="text"
								placeholder="" />
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="userName">数据库登录名</label>
						<div class="col-sm-4">
							<input class="form-control" id="userName" name="userName"
								type="text" placeholder=" " />
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="password">密码</label>
						<div class="col-sm-4">
							<input class="form-control" id="password" name="password"
								type="text" placeholder=" " />
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="driverName">驱动名</label>
						<div class="col-sm-4">
							<select class="form-control" id="driverName" name="driverName">
								<option value="0">请选择...</option>
								<option value="com.mysql.jdbc.Driver">mysql</option>
								<option value="com.microsoft.sqlserver.jdbc.SQLServerDriver">sql
									server 2005及以上</option>
								<option value="com.microsoft.jdbc.sqlserver.SQLServerDriverr">sql
									server 2000</option>
                                <option value="oracle.jdbc.driver.OracleDriver">oracl 
									</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="url">url</label>
						<div class="col-sm-4">
                        <input class="form-control" id="url" name="url"
								type="text" placeholder="" />
						</div>
					</div>


				</fieldset>
			</div>
			<div class="form-horizontal" role="form">
				<fieldset>
					<legend>操作</legend>
					<div style="margin-left: 25%">

						<input id="submit1" type="button" class="btn btn-primary"
							value="提交" /> &nbsp;&nbsp;&nbsp;&nbsp; <input type="reset"
							class="btn btn-primary" value="重置" />

					</div>
				</fieldset>
			</div>
		</form>
	</div>

	<!-- 添加表 -->

	<div id="change_tables" class="container"
		style="display: none; position: relative; top: 100px; left: 100px">
		<form id="addTable" action="DataBaseServlet?method=addTable"
			method="post">
			<div class="form-horizontal" role="form">
				<fieldset>
					<legend>添加表</legend>
					<div class="form-group">
						<label class="col-sm-2 control-label">数据库名</label>
						<div class="col-sm-4">
							<select class="form-control" id="dbName_table"
								name="dbName_table">
								<option value="0">请选择...</option>

							</select>
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label" for="tableName">表名</label>
						<div class="col-sm-4">
							<input class="form-control" id="tableName" name="tableName"
								type="text" placeholder="" />
						</div>
					</div>



				</fieldset>
			</div>
			<div class="form-horizontal" role="form">
				<fieldset>
					<legend>操作</legend>
					<div style="margin-left: 25%">
						<input id="submit2" type="button" class="btn btn-primary"
							value="提交" /> &nbsp;&nbsp;&nbsp;&nbsp; <input type="reset"
							class="btn btn-primary" value="重置" />
					</div>
				</fieldset>
			</div>
		</form>

	</div>

	<!-- 添加字段 -->
	<div id="change_fields" class="container"
		style="display: none; position: relative; top: 100px; left: 100px">

		<form id="addField" action="DataBaseServlet?method=addField"
			method="post">
			<div class="form-horizontal" role="form">
				<fieldset>
					<legend>添加字段</legend>
					<div class="form-group">
						<label class="col-sm-2 control-label">数据库名</label>
						<div class="col-sm-4">
							<select class="form-control" id="dbName_field"
								name="dbName_field">
								<option value="0">请选择...</option>

							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">表名</label>
						<div class="col-sm-4">
							<select class="form-control" id="tableid_field"
								name="tableid_field">
								<option value="0">请选择...</option>

							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="fieldType">字段类型</label>
						<div class="col-sm-4">
							<select class="form-control" id="fieldType" name="fieldType">
								<option value="0">请选择...</option>
								<option value="1">待监测字段</option>
								<option value="2">位置字段</option>
								<option value="3">时间戳字段</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="fieldName">字段名</label>
						<div class="col-sm-4">
							<input class="form-control" id="fieldName" name="fieldName"
								type="text" placeholder="" />
						</div>
<!--  
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="createTime">时间戳字段名</label>
						<div class="col-sm-4">
							<input class="form-control" id="createTime" name="createTime"
								type="text" placeholder="" value="TIMESTAMP" />
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label" for="location">位置或机器号字段名</label>
						<div class="col-sm-4">
							<input class="form-control" id="location" name="location"
								type="text" placeholder="" value="" />
						</div>
					</div>
-->
				</fieldset>
			</div>
			<div class="form-horizontal" role="form" style="">
				<fieldset>
					<legend>操作</legend>
					<div style="margin-left: 25%">
						<input id="submit3" type="button" class="btn btn-primary"
							value="提交" /> &nbsp;&nbsp;&nbsp;&nbsp; <input type="reset"
							class="btn btn-primary" value="重置" />
					</div>
				</fieldset>
			</div>
		</form>
	</div>

</body>
</html>